﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using DTO;
//using System.Reflection;
//using Microsoft.Office.Interop.Excel;
using System.IO; // Using to generate spreadsheet
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Web;
using Moved;
using System.Linq;

namespace WebChamCong
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private Int32 _departmentID;
        private int userID;
        private DTO.BoPhanDTO _departObj;
        private NhanVienDTO _currentUser;
        private DateTime _currentDate;
        double TotalMealAfter = 0;
        double TotalMealEven = 0;
        double TotalTimeStandar = 0;
        double TotalWorkedTime = 0;
        double TotalLateTime = 0;
        double TotalEarlyTime = 0;
        double TotalEatLateTime = 0;


        protected void Page_PreLoad(object sender, EventArgs e)
        {
            //LoadShift();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["departmentID"] == null)
            {
                this._departmentID = (this.Request["id"] == null) ? 1 : Int32.Parse(this.Request["id"].ToString());
            }
            else
            {
                string localstr = Session["departmentID"].ToString();
                string[] str = localstr.Split('&');
                if (str.Length >= 1)
                {
                    DTO.RoleDTO dd = BUS.RolesBUS.getRoleByID(int.Parse(str[0]));
                    if (dd.UserRoles != "-1")
                    {
                        if (dd.UserRoles == null)
                        {
                            this._departmentID = (this.Request["id"] == null) ? 1 : Int32.Parse(this.Request["id"].ToString());
                        }
                        else
                        {
                            this._departmentID = (this.Request["id"] == null) ? int.Parse(dd.UserRoles) : Int32.Parse(this.Request["id"].ToString());
                        }

                    }
                    else
                    {
                        DTO.NhanVienDTO dt = BUS.NhanVienBUS.getUserIDByName(Session["username"].ToString());
                        this._departmentID = (this.Request["id"] == null) ? dt.DepartmentId : Int32.Parse(this.Request["id"].ToString());
                    }
                }


            }
            Int32 localDepartID = this._departmentID;

            DTO.BoPhanDTO localDepart = BUS.BoPhanBUS.GetBoPhanById(localDepartID);



            this._departObj = localDepart;

            if (!this.IsPostBack)
            {
                this._currentDate = DateTime.Now;

                //DTO.BoPhanDTO localDepart = BUS.BoPhanBUS.GetBoPhanById(localDepartID);

                //this._departObj = localDepart;

                this.lb_numEmps.Text = BUS.NhanVienBUS.CountNhanVienByDepartID(localDepartID).ToString();

                //WebControl control = this.Button1;

                //LabelGridView.ConfirmMessageBox(ref control, "Ban co muon xuat export hay ko ?", this.Button1.ClientID,
                //                                "onclick");

                // Load department Name)));
                this.lb_departName.Text = localDepart.DepartmentName;

                // Load month dropdownlist
                LoadMonths(this._currentDate);

                // Load years dropdownlist
                LoadYears(this._currentDate);

                // Default load list users with default
                //LoadUsers(this._currentDate, localDepartID);

                // Load list shift type
                //LoadShift();

                LoadShift();

                LoadUsers(this._currentDate, localDepartID);

                return;
            }



            // Get current value in ddl_month
            int currentMonth = Int32.Parse(this.ddl_month.Items[this.ddl_month.SelectedIndex].Value.ToString());

            // Get current value in ddl_year
            int currentYear = Int32.Parse(this.ddl_year.Items[this.ddl_year.SelectedIndex].Value.ToString());

            int currentDay = 1;

            this._currentDate = new DateTime(currentYear, currentMonth, currentDay);

            LoadUsers(this._currentDate, localDepartID);
        }

        private void LoadShift()
        {
            int i = 1;
            List<CaDTO> shifts = BUS.CaBUS.GetAllShift();
            String temp = null;

            DTO.CaDTO time = new DTO.CaDTO();

            foreach (CaDTO caItem in shifts)
            {
                int value = caItem.ShiffID;
                //HtmlGenericControl div = new HtmlGenericControl("div");
                ////ListItem lItem = new ListItem();
                ////lItem.Text = caItem.ShiffName;
                ////lItem.Value = caItem.ShiffID.ToString();

                //RadioButton rd = new RadioButton();
                //rd.Text = caItem.ShiffName;
                //rd.GroupName = "group_shift";

                //LinkButton l_change = new LinkButton();
                //l_change.Text = " Thay đổi";
                //l_change.CssClass = "click";
                //l_change.PostBackUrl = "#";
                //LinkButton l_update = new LinkButton();
                //l_update.Text = " Cập nhật";
                //l_update.Visible = false;
                //l_update.CssClass = "show";

                //LinkButton l_cancel = new LinkButton();
                //l_cancel.Text = " Hủy";
                //l_cancel.Visible = false;
                //l_cancel.CssClass = "show";

                ////rdListShift.Items.Add(lItem);

                //div.Controls.Add(rd);
                //div.Controls.Add(l_change);
                //div.Controls.Add(l_update);
                //div.Controls.Add(l_cancel);
                //div_list.Controls.Add(div);

                String formatHTML = @"<div>";
                String radioHTML = @"<input class='show' ID='" + i + "' value ='" + value + "'type='radio' name='GroupShift'/>" + caItem.ShiffName;
                String anchorHTML1 = @"<a id='a_" + i + "' class='appear1' style='display:none;text-align:center;' title='" + i + "' href='#'> Edit </a>";
                String anchorHTML2 = @"<a id='a1_" + i + "' class='appear2' style='display:none;text-align:center;' href='#' > Update</a>";
                String anchorHTML3 = @"&nbsp;<a id='a2_" + i + "' class='appear3' style='display:none;text-align:center;' href='#'> Cancel</a>";
                String endFormatHTML = @"</div>";
                temp += formatHTML + radioHTML + anchorHTML1 + anchorHTML2 + anchorHTML3 + endFormatHTML;

                i++;
                //Panel p_list = new Panel();
                // p_list.

                // i = this.rdListShift.Items.Count;
            }

            String formatHTML2 = @"<div>";
            String radioHTML2 = @"<input class='show' ID='" + i + "' value ='13'type='radio' name='GroupShift'/>" + "Ngày nghỉ";
            String anchorHTML4 = @"<a id='a_" + i + "' class='appear1' style='display:none;text-align:center;' title='" + i + "' href='#'> Edit </a>";
            String anchorHTML5 = @"<a id='a1_" + i + "' class='appear2' style='display:none;text-align:center;' href='#' > Update</a>";
            String anchorHTML6 = @"&nbsp;<a id='a2_" + i + "' class='appear3' style='display:none;text-align:center;' href='#'> Cancel</a>";
            String endFormatHTML2 = @"</div>";
            temp += formatHTML2 + radioHTML2 + anchorHTML4 + anchorHTML5 + anchorHTML6 + endFormatHTML2;
            div_list.InnerHtml = temp;

            // div_list.InnerHtml = "<input> ";


            // div.Controls.Add(rdListShift);
            //ListItem lastItem = new ListItem();
            //lastItem.Text = "Ngày Nghỉ";
            //lastItem.Value =  ((int)CaDTO.ShiftE.Default).ToString();
            //this.rdListShift.Items.Add(lastItem);

            //this.rdListShift.SelectedIndex = 0;
            //Button a = new Button();
            //a.Text = "asdf";
            //a.Visible = true;
            // p_list.Controls.Add(a);

        }


        private void LoadMonths(DateTime currentDate)
        {
            DateTime localTime = currentDate;

            this.ddl_month.Items.Clear();

            for (int i = 1; i < 13; i++)
            {
                this.ddl_month.Items.Add(new ListItem("Tháng " + i, i.ToString()));

                // Check if month 's current day has value
                if (localTime.Month == i)
                {
                    this.ddl_month.SelectedIndex = i - 1;
                }

            }

        }

        private void LoadYears(DateTime currentDate)
        {
            DateTime localTime = currentDate;

            this.ddl_year.Items.Clear();

            for (int i = 2000; i < 2051; i++)
            {
                this.ddl_year.Items.Add(new ListItem("Năm " + i, i.ToString()));

                // Check if year 's current day has value
                if (localTime.Year == i)
                {
                    this.ddl_year.SelectedIndex = i - 2000;
                }

            }
        }

        private void LoadUsers(DateTime currentDate, Int32 departmentID)
        {
            DateTime localTime = currentDate;

            Int32 localDepartID = departmentID;

            System.Data.DataTable listUsers = new System.Data.DataTable();

            // **********************************
            // Create columns to datatable
            // **********************************
            listUsers.Columns.Add("ID", System.Type.GetType("System.String"));
            listUsers.Columns.Add("Ho Ten", System.Type.GetType("System.String"));

            for (int i = 1; i < DateTime.DaysInMonth(localTime.Year, localTime.Month) + 1; i++)
            {
                listUsers.Columns.Add(i.ToString(), System.Type.GetType("System.String"));
            }

            listUsers.Columns.Add("Cham Cong", System.Type.GetType("System.String"));

            // **********************************
            // Add data into datatable
            // **********************************

            // Get users ' department from database
            //List<DTO.NhanVienDTO>listU = BUS.NhanVienBUS.GetNhanViensByDepartID(localDepartID);
            List<DTO.WitSchedule> listU = BUS.LichBUS.GetScheduleByDepartAndMonth(localDepartID, localTime);

            if (listU.Count < 1)
            {
                this.btn_CreateSchedule.Visible = true;
                this.BtnRefresh.Visible = false;
            }
            else
            {
                this.btn_CreateSchedule.Visible = false;
                this.BtnRefresh.Visible = true;
            }

            foreach (DTO.WitSchedule item in listU)
            {
                DataRow newRow = listUsers.NewRow();
                newRow["ID"] = item.UserId;
                newRow["Ho Ten"] = item.UserName;

                newRow["1"] = item.Day1;
                newRow["2"] = item.Day2;
                newRow["3"] = item.Day3;
                newRow["4"] = item.Day4;
                newRow["5"] = item.Day5;
                newRow["6"] = item.Day6;
                newRow["7"] = item.Day7;
                newRow["8"] = item.Day8;
                newRow["9"] = item.Day9;
                newRow["10"] = item.Day10;

                newRow["11"] = item.Day11;
                newRow["12"] = item.Day12;
                newRow["13"] = item.Day13;
                newRow["14"] = item.Day14;
                newRow["15"] = item.Day15;
                newRow["16"] = item.Day16;
                newRow["17"] = item.Day17;
                newRow["18"] = item.Day18;
                newRow["19"] = item.Day19;
                newRow["20"] = item.Day20;

                newRow["21"] = item.Day21;
                newRow["22"] = item.Day22;
                newRow["23"] = item.Day23;
                newRow["24"] = item.Day24;
                newRow["25"] = item.Day25;
                newRow["26"] = item.Day26;
                newRow["27"] = item.Day27;

                int totalDays = DateTime.DaysInMonth(localTime.Year, localTime.Month);

                if (totalDays <= 28)
                {
                    newRow["28"] = item.Day28;
                }
                else if (totalDays <= 29)
                {
                    newRow["28"] = item.Day28;
                    newRow["29"] = item.Day29;
                }

                else if (totalDays <= 30)
                {
                    newRow["28"] = item.Day28;
                    newRow["29"] = item.Day29;
                    newRow["30"] = item.Day30;
                }
                else
                {
                    newRow["28"] = item.Day28;
                    newRow["29"] = item.Day29;
                    newRow["30"] = item.Day30;
                    newRow["31"] = item.Day31;
                }

                listUsers.Rows.Add(newRow);

                listUsers.AcceptChanges();
            }

            this.gv_listUsers.Columns.Clear();

            foreach (DataColumn item in listUsers.Columns)
            {
                if (item.ColumnName.Equals("Cham Cong"))
                {
                    ButtonField buField = new ButtonField();
                    buField.ButtonType = ButtonType.Button;
                    buField.HeaderText = "Chấm Công";

                    buField.Text = "Chấm Công";
                    buField.CommandName = "chamcong";
                    this.gv_listUsers.Columns.Add(buField);


                }
                else if (item.ColumnName.Equals("ID") || item.ColumnName.Equals("Ho Ten"))
                {
                    BoundField bField = new BoundField();
                    bField.DataField = item.ColumnName;
                    if (item.ColumnName.Equals("Ho Ten"))
                    {
                        bField.HeaderText = "Họ Tên";
                    }
                    else
                    {
                        bField.HeaderText = item.ColumnName;
                    }
                    this.gv_listUsers.Columns.Add(bField);
                }
                else
                {
                    TemplateField teField = new TemplateField();
                    teField.HeaderText = item.ColumnName;
                    teField.ItemTemplate = new LabelGridView(DataControlRowType.DataRow, item.ColumnName, "", "Label", localTime);
                    this.gv_listUsers.Columns.Add(teField);
                }

            }

            //this.gv_listUsers.PageSize = 10;

            //this.gv_listUsers.AllowPaging = true;

            //this.gv_listUsers.AutoGenerateColumns = false;

            this.gv_listUsers.DataKeyNames = new string[] { "ID" };

            this.gv_listUsers.DataSource = listUsers;

            this.gv_listUsers.DataBind();

        }

        protected void CreateSchedule_OnClick(object sender, EventArgs e)
        {
            int localDepart = this._departmentID;

            // Get current value in ddl_month
            int currentMonth = Int32.Parse(this.ddl_month.Items[this.ddl_month.SelectedIndex].Value.ToString());

            // Get current value in ddl_year
            int currentYear = Int32.Parse(this.ddl_year.Items[this.ddl_year.SelectedIndex].Value.ToString());

            int currentDay = 1;

            DateTime localStartDay = new DateTime(currentYear, currentMonth, currentDay);
            DateTime localEndDay = new DateTime(currentYear, currentMonth, DateTime.DaysInMonth(currentYear, currentMonth));

            BUS.LichBUS.CreateScheduleByDepartAndDate(localDepart, localStartDay, localEndDay);

            this.LoadUsers(localStartDay, localDepart);

        }

        protected void OkButton_OnClick(object sender, EventArgs e)
        {

            int localDepart = this._departmentID;
            DateTime localDatetime = this._currentDate;
            // Get user id
            int uid = Int32.Parse(this.hf_u.Value);
            // Get day
            int day = Int32.Parse(this.hf_d.Value);
            // Get month
            int month = Int32.Parse(this.hf_m.Value);
            // Get year
            int year = Int32.Parse(this.hf_y.Value);
            // Get value
            int value = Int32.Parse(Request.Form["GroupShift"].ToString());
            //int value = Int32.Parse(this.div_list.Attributes(value));

            // Update data into schedule
            BUS.LichBUS.UpdateScheduleByIDDayMonthYear(uid, day, month, year, value);

            this.ModalPopupExtender1.Hide();

            this.LoadUsers(localDatetime, localDepart);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox(this.gv_listUsers.SelectedIndex.ToString());
        }

        protected void ddl_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 localDepart = this._departmentID;

            // Get current value in ddl_month
            int currentMonth = Int32.Parse(this.ddl_month.Items[this.ddl_month.SelectedIndex].Value.ToString());

            // Get current value in ddl_year
            int currentYear = Int32.Parse(this.ddl_year.Items[this.ddl_year.SelectedIndex].Value.ToString());

            int currentDay = 1;

            DateTime currentDate = new DateTime(currentYear, currentMonth, currentDay);

            // Rebind ddl_month
            this.LoadMonths(currentDate);

            // Rebind ddl_year
            this.LoadYears(currentDate);

            // Rebind gv_users
            this.LoadUsers(currentDate, localDepart);
        }

        protected void ddl_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 localDepart = this._departmentID;

            // Get current value in ddl_month
            int currentMonth = Int32.Parse(this.ddl_month.Items[this.ddl_month.SelectedIndex].Value.ToString());

            // Get current value in ddl_year
            int currentYear = Int32.Parse(this.ddl_year.Items[this.ddl_year.SelectedIndex].Value.ToString());

            int currentDay = 1;

            DateTime currentDate = new DateTime(currentYear, currentMonth, currentDay);

            // Rebind ddl_month
            this.LoadMonths(currentDate);

            // Rebind ddl_year
            this.LoadYears(currentDate);

            // Rebind gv_users
            this.LoadUsers(currentDate, localDepart);
        }

        protected void gv_listUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dataItem = (DataRowView)e.Row.DataItem;
                // Col ID
                HyperLink colID = new HyperLink();
                colID.Text = dataItem[0].ToString();
                colID.NavigateUrl = "EmployeeDetails.aspx?id=" + dataItem[0].ToString() + "&month=" + ddl_month.SelectedValue.ToString() + "&year=" + ddl_year.SelectedValue.ToString();
                e.Row.Cells[0].Controls.Add(colID);

                // Col Name
                HyperLink colName = new HyperLink();
                colName.Text = dataItem[1].ToString();
                colName.NavigateUrl = "EmployeeDetails.aspx?id=" + dataItem[0].ToString() + "&month=" + ddl_month.SelectedValue.ToString() + "&year=" + ddl_year.SelectedValue.ToString();
                colName.NavigateUrl = "EmployeeDetails.aspx?id=" + dataItem[0].ToString() + "&month=" + ddl_month.SelectedValue.ToString() + "&year=" + ddl_year.SelectedValue.ToString();
                e.Row.Cells[1].Controls.Add(colName);

            }

        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }



        private void Export_Employee(int empId, DateTime date)
        {
            int localEmpId = empId;
            DateTime localDate = date;
            double localTotalMealAfter = TotalMealAfter;
            double localTotalMealEven = TotalMealEven;
            double localTotalTimeStandar = TotalTimeStandar;
            double localTotalWorkedTime = TotalWorkedTime;
            double locatTotalLateTime = TotalLateTime;
            double localTotalEarlyTime = TotalEarlyTime;
            double localTotalEatLateTime = TotalEatLateTime;
            List<DTO.UserDetail> localUserDetails = BUS.LichBUS.GetUserDetail(localEmpId, localDate, ref TotalMealAfter, ref TotalMealEven, ref TotalTimeStandar, ref TotalWorkedTime, ref TotalLateTime, ref TotalEarlyTime, ref TotalEatLateTime);
            string fileName = Server.MapPath("temp_excels/test.xls");
            ReportDocument rpObject = new ReportDocument();

            rpObject.Load(Server.MapPath("data/rp_employee.rpt"));

            //table o day
            DataTable result = BUS.LichBUS.ConvertListToDataTable(localUserDetails, ref TotalMealAfter, ref TotalMealEven, ref TotalTimeStandar, ref TotalWorkedTime, ref TotalLateTime, ref TotalEarlyTime, ref TotalEatLateTime);

            rpObject.SetDataSource(result);
            DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(this._departmentID);
            rpObject.SetParameterValue("empName", this._currentUser.FullName);
            rpObject.SetParameterValue("empDepart", bp.DepartmentName);
            rpObject.SetParameterValue("empDate", localDate);

            rpObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, fileName);

            FileInfo file = new FileInfo(fileName);

            if (file.Exists)
            {
                //Response.Clear();
                Response.Buffer = true;
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
            }
        }
        protected void gv_listUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "chamcong")
            {
                this.userID = Convert.ToInt32(this.gv_listUsers.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                NhanVienDTO objNhanVien = BUS.NhanVienBUS.GetNhanViensByID(this.userID);
                this._currentUser = objNhanVien;
                //goi o duoi day
                Export_Employee(this.userID, this._currentDate);
                //exportReport(this.userID, this._currentUser);

            }
            else if (e.CommandName == "choose_shift")
            {
                string commandArgu = e.CommandArgument.ToString();
                string[] strings = commandArgu.Split(',');

                this.hf_u.Value = strings[0];
                this.hf_d.Value = strings[1];
                this.hf_m.Value = strings[2];
                this.hf_y.Value = strings[3];
               
                temp_UserID.Value = hf_u.Value;
                temp_Day.Value = hf_d.Value;

                this.hf_ti.Value = BUS.LichBUS.GetHday1(Int32.Parse(temp_UserID.Value), Int32.Parse(temp_Day.Value));
                this.hf_to.Value = BUS.LichBUS.GetHday2(Int32.Parse(temp_UserID.Value), Int32.Parse(temp_Day.Value));

                temp_tin.Value = hf_ti.Value;
                temp_tout.Value = hf_to.Value;

                String temp = "";
                String input2 = @"<input type='text' value='" + temp_Day.Value + "' style='display:none' id='temp_Day' size='6' />";
                String input = @"<input type='text' value='" + temp_UserID.Value + "' style='display:none' id='temp_UserID' size='6' />";
                String input3 = @"<input type='text' value='" + temp_tin.Value + "' style='display:none' id='temp_tin' size='6' />";
                String input4 = @"<input type='text' value='" + temp_tout.Value + "' style='display:none' id='temp_tout' size='6' />";
                temp += input + input2 + input3 + input4;
                div_temp.InnerHtml = temp;
                this.ModalPopupExtender1.Show();
            }
        }

        protected void gv_listUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DateTime localTime = this._currentDate;
            int localDepart = this._departmentID;

            this.gv_listUsers.PageIndex = e.NewPageIndex;

            this.LoadUsers(localTime, localDepart);
        }
        private void Export_Cong(int DepartmentID, DateTime date)
        {
            int localDepartId = DepartmentID;
            DateTime localDate = date;
            string fileName = Server.MapPath("temp_excels/test.xls");
            ReportDocument rpObject = new ReportDocument();

            rpObject.Load(Server.MapPath("data/lapcong.rpt"));


            //table o day
            DataTable result = BUS.LichBUS.XuatThietLapCong(localDepartId, localDate);
            rpObject.SetDataSource(result);
            rpObject.SetParameterValue("month", localDate.Month);

            rpObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, fileName);

            FileInfo file = new FileInfo(fileName);

            if (file.Exists)
            {
                //Response.Clear();
                Response.Buffer = true;
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
            }
        }

        private void RefreshUsers(DateTime currentDate, Int32 departmentID)
        {
            DateTime localTime = currentDate;

            Int32 localDepartID = departmentID;

            System.Data.DataTable listUsers = new System.Data.DataTable();

            // **********************************
            // Create columns to datatable
            // **********************************
            listUsers.Columns.Add("ID", System.Type.GetType("System.String"));
            listUsers.Columns.Add("Ho Ten", System.Type.GetType("System.String"));

            for (int i = 1; i < DateTime.DaysInMonth(localTime.Year, localTime.Month) + 1; i++)
            {
                listUsers.Columns.Add(i.ToString(), System.Type.GetType("System.String"));
            }

            listUsers.Columns.Add("Cham Cong", System.Type.GetType("System.String"));

            // **********************************
            // Add data into datatable
            // **********************************

            // Get users ' department from database
            //List<DTO.NhanVienDTO>listU = BUS.NhanVienBUS.GetNhanViensByDepartID(localDepartID);
            List<DTO.WitSchedule> listU = BUS.LichBUS.GetScheduleByDepartAndMonth(localDepartID, localTime);

            if (listU.Count < 1)
            {
                this.btn_CreateSchedule.Visible = true;
            }
            else
            {
                this.btn_CreateSchedule.Visible = false;
            }

            foreach (DTO.WitSchedule item in listU)
            {
                DataRow newRow = listUsers.NewRow();
                newRow["ID"] = item.UserId;
                newRow["Ho Ten"] = item.UserName;

                newRow["1"] = item.Day1;
                newRow["2"] = item.Day2;
                newRow["3"] = item.Day3;
                newRow["4"] = item.Day4;
                newRow["5"] = item.Day5;
                newRow["6"] = item.Day6;
                newRow["7"] = item.Day7;
                newRow["8"] = item.Day8;
                newRow["9"] = item.Day9;
                newRow["10"] = item.Day10;

                newRow["11"] = item.Day11;
                newRow["12"] = item.Day12;
                newRow["13"] = item.Day13;
                newRow["14"] = item.Day14;
                newRow["15"] = item.Day15;
                newRow["16"] = item.Day16;
                newRow["17"] = item.Day17;
                newRow["18"] = item.Day18;
                newRow["19"] = item.Day19;
                newRow["20"] = item.Day20;

                newRow["21"] = item.Day21;
                newRow["22"] = item.Day22;
                newRow["23"] = item.Day23;
                newRow["24"] = item.Day24;
                newRow["25"] = item.Day25;
                newRow["26"] = item.Day26;
                newRow["27"] = item.Day27;

                int totalDays = DateTime.DaysInMonth(localTime.Year, localTime.Month);

                if (totalDays <= 28)
                {
                    newRow["28"] = item.Day28;
                }
                else if (totalDays <= 29)
                {
                    newRow["28"] = item.Day28;
                    newRow["29"] = item.Day29;
                }
                else if (totalDays <= 30)
                {
                    newRow["28"] = item.Day28;
                    newRow["29"] = item.Day29;
                    newRow["30"] = item.Day30;
                }
                else
                {
                    newRow["28"] = item.Day28;
                    newRow["29"] = item.Day29;
                    newRow["30"] = item.Day30;
                    newRow["31"] = item.Day31;
                }

                listUsers.Rows.Add(newRow);

                listUsers.AcceptChanges();
            }

            this.gv_listUsers.Columns.Clear();

            foreach (DataColumn item in listUsers.Columns)
            {
                if (item.ColumnName.Equals("Cham Cong"))
                {
                    ButtonField buField = new ButtonField();
                    buField.ButtonType = ButtonType.Button;
                    buField.HeaderText = "Chấm Công";

                    buField.Text = "Chấm Công";
                    buField.CommandName = "chamcong";
                    this.gv_listUsers.Columns.Add(buField);


                }
                else if (item.ColumnName.Equals("ID") || item.ColumnName.Equals("Ho Ten"))
                {
                    BoundField bField = new BoundField();
                    bField.DataField = item.ColumnName;
                    if (item.ColumnName.Equals("Ho Ten"))
                    {
                        bField.HeaderText = "Họ Tên";
                    }
                    else
                    {
                        bField.HeaderText = item.ColumnName;
                    }
                    this.gv_listUsers.Columns.Add(bField);
                }
                else
                {
                    TemplateField teField = new TemplateField();
                    teField.HeaderText = item.ColumnName;
                    teField.ItemTemplate = new LabelGridView(DataControlRowType.DataRow, item.ColumnName, "", "Label", localTime);
                    this.gv_listUsers.Columns.Add(teField);
                }

            }

            //this.gv_listUsers.PageSize = 10;

            //this.gv_listUsers.AllowPaging = true;

            //this.gv_listUsers.AutoGenerateColumns = false;

            this.gv_listUsers.DataKeyNames = new string[] { "ID" };

            this.gv_listUsers.DataSource = listUsers;

            this.gv_listUsers.DataBind();

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            Export_Cong(this._departmentID, this._currentDate);
        }

        protected void gv_listUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.gv_listUsers.SelectedIndex;
        }

        protected void Refresh_OnClick(object sender, EventArgs e)
        {
            int localDepart = this._departmentID;

            // Get current value in ddl_month
            int currentMonth = Int32.Parse(this.ddl_month.Items[this.ddl_month.SelectedIndex].Value.ToString());

            // Get current value in ddl_year
            int currentYear = Int32.Parse(this.ddl_year.Items[this.ddl_year.SelectedIndex].Value.ToString());

            int currentDay = 1;

            DateTime localStartDay = new DateTime(currentYear, currentMonth, currentDay);
            DateTime localEndDay = new DateTime(currentYear, currentMonth, DateTime.DaysInMonth(currentYear, currentMonth));

            // BUS.LichBUS.InsertEmpIntoDepSchedule(localDepart, localStartDay, localEndDay);

            this.LoadUsers(localStartDay, localDepart);
        }


        [WebMethod]
        public static bool ClickMe(String temp_UserID, String temp_Day, String time3, String time4)
        {


            int i = 0;
            if ((time3.ElementAt(i) == '0' || time3.ElementAt(i) == '1' || time3.ElementAt(i) == '2' || time3.ElementAt(i) == '3' || time3.ElementAt(i) == '4' || time3.ElementAt(i) == '5' || time3.ElementAt(i) == '6' || time3.ElementAt(i) == '7' || time3.ElementAt(i) == '8' || time3.ElementAt(i) == '9' || time3.ElementAt(i) == ':')
                && (time4.ElementAt(i) == '0' || time4.ElementAt(i) == '1' || time4.ElementAt(i) == '2' || time4.ElementAt(i) == '3' || time4.ElementAt(i) == '4' || time4.ElementAt(i) == '5' || time4.ElementAt(i) == '6' || time4.ElementAt(i) == '7' || time4.ElementAt(i) == '8' || time4.ElementAt(i) == '9' || time4.ElementAt(i) == ':'))
            {
                i++;
                if ((time3.ElementAt(i) == '0' || time3.ElementAt(i) == '1' || time3.ElementAt(i) == '2' || time3.ElementAt(i) == '3' || time3.ElementAt(i) == '4' || time3.ElementAt(i) == '5' || time3.ElementAt(i) == '6' || time3.ElementAt(i) == '7' || time3.ElementAt(i) == '8' || time3.ElementAt(i) == '9' || time3.ElementAt(i) == ':')
                && (time4.ElementAt(i) == '0' || time4.ElementAt(i) == '1' || time4.ElementAt(i) == '2' || time4.ElementAt(i) == '3' || time4.ElementAt(i) == '4' || time4.ElementAt(i) == '5' || time4.ElementAt(i) == '6' || time4.ElementAt(i) == '7' || time4.ElementAt(i) == '8' || time4.ElementAt(i) == '9' || time4.ElementAt(i) == ':'))
                {
                    i++;
                    if ((time3.ElementAt(i) == '0' || time3.ElementAt(i) == '1' || time3.ElementAt(i) == '2' || time3.ElementAt(i) == '3' || time3.ElementAt(i) == '4' || time3.ElementAt(i) == '5' || time3.ElementAt(i) == '6' || time3.ElementAt(i) == '7' || time3.ElementAt(i) == '8' || time3.ElementAt(i) == '9' || time3.ElementAt(i) == ':')
                && (time4.ElementAt(i) == '0' || time4.ElementAt(i) == '1' || time4.ElementAt(i) == '2' || time4.ElementAt(i) == '3' || time4.ElementAt(i) == '4' || time4.ElementAt(i) == '5' || time4.ElementAt(i) == '6' || time4.ElementAt(i) == '7' || time4.ElementAt(i) == '8' || time4.ElementAt(i) == '9' || time4.ElementAt(i) == ':'))
                    {
                        i++;
                        if ((time3.ElementAt(i) == '0' || time3.ElementAt(i) == '1' || time3.ElementAt(i) == '2' || time3.ElementAt(i) == '3' || time3.ElementAt(i) == '4' || time3.ElementAt(i) == '5' || time3.ElementAt(i) == '6' || time3.ElementAt(i) == '7' || time3.ElementAt(i) == '8' || time3.ElementAt(i) == '9' || time3.ElementAt(i) == ':')
                && (time4.ElementAt(i) == '0' || time4.ElementAt(i) == '1' || time4.ElementAt(i) == '2' || time4.ElementAt(i) == '3' || time4.ElementAt(i) == '4' || time4.ElementAt(i) == '5' || time4.ElementAt(i) == '6' || time4.ElementAt(i) == '7' || time4.ElementAt(i) == '8' || time4.ElementAt(i) == '9' || time4.ElementAt(i) == ':'))
                        {
                            i++;
                            if ((time3.ElementAt(i) == '0' || time3.ElementAt(i) == '1' || time3.ElementAt(i) == '2' || time3.ElementAt(i) == '3' || time3.ElementAt(i) == '4' || time3.ElementAt(i) == '5' || time3.ElementAt(i) == '6' || time3.ElementAt(i) == '7' || time3.ElementAt(i) == '8' || time3.ElementAt(i) == '9' || time3.ElementAt(i) == ':')
                && (time4.ElementAt(i) == '0' || time4.ElementAt(i) == '1' || time4.ElementAt(i) == '2' || time4.ElementAt(i) == '3' || time4.ElementAt(i) == '4' || time4.ElementAt(i) == '5' || time4.ElementAt(i) == '6' || time4.ElementAt(i) == '7' || time4.ElementAt(i) == '8' || time4.ElementAt(i) == '9' || time4.ElementAt(i) == ':'))
                            {

                                BUS.NhanVienBUS.InsExtTime(Int32.Parse(temp_UserID), Int32.Parse(temp_Day), time3, time4);
                                
                            }
                        }
                    }
                }
                return true;
            }
            else
                
                return false;
            
        }

        /*[WebMethod]
        public static string ClickMe2(String time4)
        {
            return time4;
        }*/

    }
}